/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.hb.SpdcfDAOImpl;
import com.wii.elpro.dao.hb.QuotationDAOImpl;
import com.wii.elpro.dao.hb.ScatteredFunction;
import com.wii.elpro.entity.Quotation;
import com.wii.elpro.entity.Spdcf;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author hp02
 */
public class QuotationGetSpdcfAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        String forward;
        HttpSession sess = request.getSession(true);
        ActionMessages messages;
        try {
            List<Spdcf> spdcf = new SpdcfDAOImpl().getSpdcf();
//            List<Spdcf> listSpdcf = new ArrayList<Spdcf>();
//            List<Spdcf> sp = new ArrayList<Spdcf>();
            List<Quotation> quotations = new QuotationDAOImpl().getQuotations();
//            for(Quotation q:quotations) {
//                sp.add(q.getSpdcf());
//            }
//            
////            This is so bad codes, to many loop
//            for(Spdcf s:spdcf) {
//                if(quotations.size()!=0) {
//                    for(Quotation q:quotations) {
//                        if(!sp.contains(s) && s.getWasKonfirmByFinance()==1 && s.getWasKonfirmByProduction()==1) {
//                            listSpdcf.add(s);
////                            System.out.println("------------------->DONG");
//                            break;
//                        }
//                    }
//                } else {
//                    listSpdcf.add(s);
//                }
//            }
            request.setAttribute("listSpdcf", ScatteredFunction.getReadySpdcf(spdcf, quotations));
            forward = "success";
        } catch (Exception ex) {
            messages = new ActionMessages();
            messages.add("message", new ActionMessage("customMessage", "Application is fail to retrieve data. Please contact my developer."));
            saveMessages(sess, messages);
            ex.printStackTrace();
            forward = "fail";
        }
        return mapping.findForward(forward);
    }
}
